import Vue from 'vue'
import Vuex from 'vuex'
import {checkidentity} from "@/utils/user.js";
Vue.use(Vuex)
//参考:https://www.jianshu.com/p/506da488d2a1
/**
 * 1.系统分配
 * 2.角色分配
 * */
export default new Vuex.Store({
  state: {
    role:[],
    user:{},
    category:[],
    category2:localStorage["category2"]
      ? JSON.parse(localStorage["category2"])
      : [],
  },
  mutations: {
    checkRole(state,data){
      state.role=data
    },
    addUser(state,userData){
      state.user=userData;
    },
    //在category分类表中有数据时,因为多个页面需要共享因此放入vuex里
    setCategory(state,data){
      state.category=data;
    },
    getCategory(state){
      return state.category
    },
    setCategoryInfo(state,data){
      state.category2=data;
      localStorage.setItem('category2',JSON.stringify(data))
    }
  },
  actions:{
    getRouts(){
      return new Promise((resolve,reject)=>{
        checkidentity("/logIn","post", {"userCode":this.state.user.uname,"password":this.state.user.upwd})
          .then(rawRes=>rawRes.data)
          .then(res=>{
            this.commit("checkRole",res);
            resolve(res)
          })
      })
    }
  },
  getters:{
    getRole:state=>{
      return state.role
    },
  }
})
